package com.bzkj.basicinfo.service.impl.ncc;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.bzkj.basicinfo.domain.basic.vo.PrintSkuInfoVo;
import com.bzkj.basicinfo.domain.basic.vo.WarehouseExportVo;
import com.bzkj.basicinfo.domain.ncc.Warehouse;
import com.bzkj.basicinfo.domain.wms.WmsStoStockStateEntity;
import com.bzkj.basicinfo.domain.wms.vo.*;
import com.bzkj.basicinfo.mapper.*;
import com.bzkj.basicinfo.service.ncc.WarehouseService;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.domain.R;
import com.bzkj.common.core.domain.entity.SysUser;
import com.bzkj.common.core.domain.model.LoginUser;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.utils.DataProcessCommonUtils;
import com.bzkj.common.utils.DateUtils;
import com.bzkj.common.utils.SecurityUtils;
import com.bzkj.common.utils.StringUtils;
import com.bzkj.common.utils.poi.ExcelPDFUtil;
import com.bzkj.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.bzkj.common.utils.SecurityUtils.getUsername;

/**
 * @Author: Admin
 * @Description: 仓库服务实现类，提供仓库的业务逻辑处理
 */
@Slf4j
@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Autowired
    private WarehouseMapper warehouseMapper; // 注入仓库数据访问层

    @Autowired
    private StoAreaMapper stoAreaMapper;

    @Autowired
    private NccBasicStoStockMapper nccBasicStoStockMapper;

    /**
     * 新增仓库
     *
     * @param warehouse 新增的仓库对象
     * @return 受影响的记录数
     */
    @Transactional
    @Override
    public AjaxResult addWarehouse(Warehouse warehouse) {
        String stoCode = warehouse.getStoCode();//仓库编号
        if (stoCode == null || stoCode.trim().isEmpty()) {
            return AjaxResult.error("仓库编码不能为空");
        }
//        String def1 = warehouse.getDef1();
        Date date = new Date();
        String s = getOneId();

        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        String userName = user.getUserName();
        warehouse.setCreateTime(date);
        warehouse.setStoId(s);
        warehouse.setCreateBy(userName);
        int i = warehouseMapper.insertWarehouse(warehouse);

        if (i > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 更新仓库
     *
     * @param warehouse 更新的仓库对象
     * @return 受影响的记录数
     */
    @Override
    public AjaxResult updateWarehouse(Warehouse warehouse) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        String userName = user.getUserName();
        warehouse.setUpdateBy(userName);
        warehouse.setUpdateTime(DateUtils.getNowDate());
        int result = warehouseMapper.updateWarehouse(warehouse);// 更新仓库
        if(result > 0) {
            return AjaxResult.success("更新仓库成功");
        } else {
            return AjaxResult.error("更新仓库失败");
        }
    }

    /**
     * 根据ID查询仓库
     *
     * @param stoId 仓库的唯一标识符
     * @return 查询到的仓库对象
     */
    @Override
    public Warehouse getWarehouseById(String stoId) {
        return warehouseMapper.selectWarehouseById(stoId); // 根据ID查询仓库
    }

    /**
     * 查询所有仓库
     *
     * @return 所有仓库的列表
     */
    @Override
    public List<Warehouse> getAllWarehouses(Warehouse warehouse) {
        return warehouseMapper.selectAllWarehouses(warehouse); // 查询所有仓库
    }
    @Override
    public List<Warehouse> getAllWarehousesList(Warehouse warehouse) {
        return warehouseMapper.selectWarehouses(warehouse); // 查询所有仓库
    }

    /**
     * 删除仓库
     *
     * @param stoId 仓库的唯一标识符
     * @return 受影响的记录数
     */
    @Override
    public int deleteWarehouse(String stoId) {
//        //删除关联信息表中的数据
//        warehouseMapper.deleteStockMap(stoId);
        // 删除仓库数据
        return warehouseMapper.deleteWarehouse(stoId);
    }

    /**
     * 检查仓库是否有库区关联
     *
     * @param stoId 仓库的唯一标识符
     * @return true 如果仓库有库区关联，false 反之
     */
    @Override
    public boolean hasAreas(String stoId) {
        // 调用数据访问层的方法，检查该仓库ID是否有相关联的库区
        return stoAreaMapper.selectStoAreasByWarehouseId(stoId).size() > 0;
    }

    @Override
    public AjaxResult getNccBasicStoStock() {
        List<HashMap> basicStoStocks = nccBasicStoStockMapper.queryNccBasicStoStockAll();
        return AjaxResult.success(basicStoStocks);
    }

    @Override
    public List<HashMap<String, Object>> queryStoWareHouse(String sto_code) {
        return warehouseMapper.queryStoWareHouse(sto_code);
    }

    @Override
    public AjaxResult updateEnableStatus(String status, String stoId) {
        try {
            int i = warehouseMapper.updateEnableStatus(status, stoId);
            return i > 0 ? AjaxResult.success("修改成功") : AjaxResult.error("未找到指定数据");
        } catch (Exception e) {
            log.error("修改失败:{}", e);
            return AjaxResult.error("修改失败");
        }
    }

    @Override
    public R getWarehouseInBasic(String dataType, String sType, String wareHouseMode) {
        if (!StringUtils.isEmpty(sType))
            return R.ok(warehouseMapper.getWarehouseInBasic(dataType, wareHouseMode, sType));

        List<HashMap<String, Object>> list = warehouseMapper.getWarehouseInBasic(dataType, wareHouseMode, "1");
        List<HashMap<String, Object>> list2 = warehouseMapper.getWarehouseInBasic(dataType, wareHouseMode, "2");
        List<HashMap<String, Object>> list3 = warehouseMapper.getWarehouseInBasic(dataType, wareHouseMode, "3");
        List<HashMap<String, Object>> list4 = warehouseMapper.getWarehouseInBasic(dataType, wareHouseMode, "4");

        HashMap<String, Object> map = new HashMap<>();
        map.put("type1", list);
        map.put("type2", list2);
        map.put("type3", list3);
        map.put("type4", list4);
        return R.ok(map);
    }

    @Override
    public R getWarehouseInNoFinishBills(String dataType, String wareHouseMode, String noFinishMode) {
        if (!StringUtils.isEmpty(noFinishMode)) {
            List<HashMap<String, Object>> map = warehouseMapper.getWarehouseInNoFinishBills(dataType, wareHouseMode, noFinishMode);
            return R.ok(map);
        } else {
            List<HashMap<String, Object>> map = warehouseMapper.getWarehouseInNoFinishBills(dataType, wareHouseMode, "0");
            List<HashMap<String, Object>> map1 = warehouseMapper.getWarehouseInNoFinishBills(dataType, wareHouseMode, "1");
            HashMap<String, Object> map2 = new HashMap<>();
            map2.put("type0", map);
            map2.put("type1", map1);
            return R.ok(map2);
        }
    }

    @Override
    public R getWarehouseInStatistic(String dataType, String wareHouseMode, String wareHouseModeValue, String wareHouseName, String wareHouseValue, String senderId, String senderValue, String skuId, String skuIdValue, String sType) {
        if (!StringUtils.isEmpty(sType))
            return R.ok(warehouseMapper.getWarehouseInStatistic(dataType, wareHouseMode, wareHouseModeValue, wareHouseName, wareHouseValue, senderId, senderValue, skuId, skuIdValue, sType));

        List<HashMap<String, Object>> list1 = warehouseMapper.getWarehouseInStatistic(dataType, wareHouseMode, wareHouseModeValue, wareHouseName, wareHouseValue, senderId, senderValue, skuId, skuIdValue, "1");
        List<HashMap<String, Object>> list2 = warehouseMapper.getWarehouseInStatistic(dataType, wareHouseMode, wareHouseModeValue, wareHouseName, wareHouseValue, senderId, senderValue, skuId, skuIdValue, "2");
        List<HashMap<String, Object>> list3 = warehouseMapper.getWarehouseInStatistic(dataType, wareHouseMode, wareHouseModeValue, wareHouseName, wareHouseValue, senderId, senderValue, skuId, skuIdValue, "3");
        List<HashMap<String, Object>> list4 = warehouseMapper.getWarehouseInStatistic(dataType, wareHouseMode, wareHouseModeValue, wareHouseName, wareHouseValue, senderId, senderValue, skuId, skuIdValue, "4");
        List<HashMap<String, Object>> list5 = warehouseMapper.getWarehouseInStatistic(dataType, wareHouseMode, wareHouseModeValue, wareHouseName, wareHouseValue, senderId, senderValue, skuId, skuIdValue, "5");
        HashMap<String, Object> map = new HashMap<>();
        map.put("type1", list1);
        map.put("type2", list2);
        map.put("type3", list3);
        map.put("type4", list4);
        map.put("type5", list5);

        return R.ok(map);
    }

    @Override
    public R getWarehouseOutBasic(String dataType, String sType, String wareHouseMode) {

        if (!StringUtils.isEmpty(sType))
            return R.ok(warehouseMapper.getWarehouseOutBasic(dataType, wareHouseMode, sType));

        List<HashMap<String, Object>> list = warehouseMapper.getWarehouseOutBasic(dataType, wareHouseMode, "1");
        List<HashMap<String, Object>> list2 = warehouseMapper.getWarehouseOutBasic(dataType, wareHouseMode, "2");
        List<HashMap<String, Object>> list3 = warehouseMapper.getWarehouseOutBasic(dataType, wareHouseMode, "3");

        HashMap<String, Object> map = new HashMap<>();
        map.put("type1", list);
        map.put("type2", list2);
        map.put("type3", list3);
        return R.ok(map);
    }

    @Override
    public R getWarehouseOutNoFinishBills(String dataType, String wareHouseMode, String noFinishMode) {
        if (!StringUtils.isEmpty(noFinishMode)) {
            List<HashMap<String, Object>> map = warehouseMapper.getWarehouseOutNoFinishBills(dataType, wareHouseMode, noFinishMode);
            return R.ok(map);
        } else {
            List<HashMap<String, Object>> map = warehouseMapper.getWarehouseOutNoFinishBills(dataType, wareHouseMode, "0");
            List<HashMap<String, Object>> map1 = warehouseMapper.getWarehouseOutNoFinishBills(dataType, wareHouseMode, "1");
            HashMap<String, Object> map2 = new HashMap<>();
            map2.put("type0", map);
            map2.put("type1", map1);
            return R.ok(map2);
        }

    }

    @Override
    public R getWarehouseOutStatistic(String dataType, String wareHouseMode, String wareHouseModeValue, String wareHouseName, String wareHouseValue, String customerId, String senderValue, String skuId, String skuIdValue, String sType) {
        if (!StringUtils.isEmpty(sType))
            return R.ok(warehouseMapper.getWarehouseOutStatistic(dataType, wareHouseMode, wareHouseModeValue, wareHouseName, wareHouseValue, customerId, senderValue, skuId, skuIdValue, sType));

        List<HashMap<String, Object>> list1 = warehouseMapper.getWarehouseOutStatistic(dataType, wareHouseMode, wareHouseModeValue, wareHouseName, wareHouseValue, customerId, senderValue, skuId, skuIdValue, "1");
        List<HashMap<String, Object>> list2 = warehouseMapper.getWarehouseOutStatistic(dataType, wareHouseMode, wareHouseModeValue, wareHouseName, wareHouseValue, customerId, senderValue, skuId, skuIdValue, "2");
        List<HashMap<String, Object>> list3 = warehouseMapper.getWarehouseOutStatistic(dataType, wareHouseMode, wareHouseModeValue, wareHouseName, wareHouseValue, customerId, senderValue, skuId, skuIdValue, "3");
        List<HashMap<String, Object>> list4 = warehouseMapper.getWarehouseOutStatistic(dataType, wareHouseMode, wareHouseModeValue, wareHouseName, wareHouseValue, customerId, senderValue, skuId, skuIdValue, "4");
        List<HashMap<String, Object>> list5 = warehouseMapper.getWarehouseOutStatistic(dataType, wareHouseMode, wareHouseModeValue, wareHouseName, wareHouseValue, customerId, senderValue, skuId, skuIdValue, "5");
        List<HashMap<String, Object>> list6 = warehouseMapper.getWarehouseOutStatistic(dataType, wareHouseMode, wareHouseModeValue, wareHouseName, wareHouseValue, customerId, senderValue, skuId, skuIdValue, "6");
        HashMap<String, Object> map = new HashMap<>(6);
        map.put("type1", list1);
        map.put("type2", list2);
        map.put("type3", list3);
        map.put("type4", list4);
        map.put("type5", list5);
        map.put("type6", list6);
        return R.ok(map);
    }

    @Override
    public R findAuditBills(String billsID) {
        return R.ok(warehouseMapper.findAuditBills(billsID));
    }

    @Override
    public R findAuditSku(String skuId, String batchNo, String barCode, String beginDate, String endDate,Integer pageNum,Integer pageSize) {
        List<HashMap<String, Object>> auditSku = warehouseMapper.findAuditSku(skuId, batchNo, barCode, beginDate, endDate);
        return R.ok(DataProcessCommonUtils.getTableInfo(auditSku,pageNum,pageSize));
    }

    @Override
    public R findAuditUser(String userId, String beginDate, String endDate,Integer pageNum,Integer pageSize) {
        List<HashMap<String, Object>> auditUser = warehouseMapper.findAuditUser(userId, beginDate, endDate);
        return R.ok(DataProcessCommonUtils.getTableInfo(auditUser,pageNum,pageSize));
    }

    @Override
    public R findStatisticSto(String stoCode) {
        return R.ok(warehouseMapper.findStatisticSto(stoCode));
    }

    @Override
    public R getStoUnitQuery(String stoCode, String unitIdValue, String unitId) {
        return R.ok(warehouseMapper.getStoUnitQuery(stoCode, unitIdValue, unitId));
    }

    @Override
    public R getSkuAlarms(String alarmsMode, String vbatchNo, String drugName,String startDate, String endDate,
                          String alarmsValues, Integer pageNum,Integer pageSize) {
        List<HashMap<String, Object>> list = warehouseMapper.getSkuAlarms(alarmsMode, vbatchNo,drugName,startDate,endDate, alarmsValues);
        return R.ok(DataProcessCommonUtils.getTableInfo(list,pageNum,pageSize));
    }

    @Override
    public AjaxResult exportSkuAlarms(String alarmsMode, String vbatchNo, String drugName,String startDate, String endDate,
                                      String alarmsValues,Integer pageNum,Integer pageSize, HttpServletResponse response) {

        // 获取数据
        List<HashMap<String, Object>> mapList = warehouseMapper.getSkuAlarms(alarmsMode, vbatchNo,drugName,startDate,endDate, alarmsValues);
        List<YxlExportLowAlarmsVo> outList = new ArrayList<>();
        List<YxlExportExpAlarmsVo> inList = new ArrayList<>();
        if (alarmsMode .equals("1")) {
            outList = convertLowMapToVoList(mapList);
        }else{
            inList = convertMapToVoList(mapList);
        }
        if(CollectionUtil.isNotEmpty(outList)){
            ExcelUtil<YxlExportLowAlarmsVo> util = new ExcelUtil<>(YxlExportLowAlarmsVo.class);
            util.exportExcel(response, outList, "低库存预警", "低库存预警", "低库存预警");
        }
        if(CollectionUtil.isNotEmpty(inList)){
            ExcelUtil<YxlExportExpAlarmsVo> util = new ExcelUtil<>(YxlExportExpAlarmsVo.class);
            util.exportExcel(response, inList, "有效期预警", "有效期预警", "有效期预警");
        }
        return AjaxResult.success();
    }

    // 获取指定类的所有属性名
    private String[] getAllColumns(Class<?> clazz) {
        List<String> columns = new ArrayList<>();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            columns.add(field.getName());
        }
        return columns.toArray(new String[0]);
    }
    /**
     * 安全地将Map列表转换为ExportSkuAlarmsVo列表
     * @param rows 原始数据行
     * @return 转换后的VO列表
     */
    private List<YxlExportExpAlarmsVo> convertMapToVoList(Object rows) {
        if (rows == null) {
            return Collections.emptyList();
        }

        if (!(rows instanceof List<?>)) {
            log.error("期望List类型数据，实际类型: {}", rows.getClass().getName());
            return Collections.emptyList();
        }

        List<?> rowList = (List<?>) rows;
        List<YxlExportExpAlarmsVo> resultList = new ArrayList<>(rowList.size());

        for (Object item : rowList) {
            if (item instanceof Map<?, ?>) {
                Map<?, ?> rowMap = (Map<?, ?>) item;
                YxlExportExpAlarmsVo vo = new YxlExportExpAlarmsVo();
                resultList.add(vo);
            }
        }

        return resultList;
    }
    /**
     * 安全地将Map列表转换为ExportSkuAlarmsVo列表
     * @param rows 原始数据行
     * @return 转换后的VO列表
     */
    private List<YxlExportLowAlarmsVo> convertLowMapToVoList(Object rows) {
        if (rows == null) {
            return Collections.emptyList();
        }

        if (!(rows instanceof List<?>)) {
            log.error("期望List类型数据，实际类型: {}", rows.getClass().getName());
            return Collections.emptyList();
        }

        List<?> rowList = (List<?>) rows;
        List<YxlExportLowAlarmsVo> resultList = new ArrayList<>(rowList.size());

        for (Object item : rowList) {
            if (item instanceof Map<?, ?>) {
                Map<?, ?> rowMap = (Map<?, ?>) item;
                YxlExportLowAlarmsVo vo = new YxlExportLowAlarmsVo();
                resultList.add(vo);
            }
        }

        return resultList;
    }
    /**
     * 出库打印模板
     *
     * @return
     */
    @Override
    public R getWarehouseOutDetailProductPrint(List<String> cgeneralbid) {
        if (!cgeneralbid.isEmpty()) {
            //根据ncc出库单据查询产成品出库单据
            List<HashMap<String, Object>> print = warehouseMapper.getWarehouseOutDetailProductPrint(cgeneralbid);
            BigDecimal totalPrice = BigDecimal.ZERO;
            for (HashMap<String, Object> item : print) {
                Object taxValueObj = item.get("norigtaxmny");//主含税价格
                if (taxValueObj != null) {
                    BigDecimal taxValue = new BigDecimal(taxValueObj.toString());
                    totalPrice = totalPrice.add(taxValue);
                }
            }
            totalPrice = totalPrice.setScale(2, RoundingMode.HALF_UP);
            HashMap<String, Object> map = new HashMap<>();
            map.put("totalPrice", totalPrice);
            map.put("data", print);
            return R.ok(map);
        }
        return R.fail("参数不正确");
    }

    @Override
    public R getStatisticStoUnit(String stoAreaCode, String stoUnitCode) {
        return R.ok(warehouseMapper.getStatisticStoUnit(stoAreaCode, stoUnitCode));
    }

    @Override
    public List<HashMap<String, Object>> getSkuQuery(String skuId, String batchNoMode, String pkBatchNo) {
        List<HashMap<String, Object>> list = warehouseMapper.getSkuQuery(skuId, batchNoMode, pkBatchNo);
        //日期格式处理
        dateFormat(list);
        return list;
    }

    @Autowired
    private WmsStoStockLimitationMapper wmsStoStockLimitationMapper;

    @Autowired
    private BasicBillsTypeMapMapper basicBillsTypeMapMapper;

    @Autowired
    private TableXXXSkuMapper tableXXXSkuMapper;

    @Autowired
    private WmsStoStockStateMapper wmsStoStockStateMapper;

    @Override
    public R getWarehouseStatistic(String skuclassId, String skuclassValue, String wareHouseID, String wareHouseValue, String skuID, String skuIDValue, String batchNo, String batchNoValue, String expDateNum, String expDateValue, String resetDateNum, String resetDateValue, String stockNum, String stocValue, String skuState, String skuStateValue, String sType) {
        if (!StringUtils.isEmpty(sType)) {
            return R.ok(warehouseMapper.getWarehouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, sType));
        }
        List<HashMap<String, Object>> list0 = warehouseMapper.getWarehouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, "0");
        List<HashMap<String, Object>> list1 = warehouseMapper.getWarehouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, "1");
        List<HashMap<String, Object>> list2 = warehouseMapper.getWarehouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, "2");
        List<HashMap<String, Object>> list3 = warehouseMapper.getWarehouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, "3");
        List<HashMap<String, Object>> list4 = warehouseMapper.getWarehouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, "4");
        List<HashMap<String, Object>> list5 = warehouseMapper.getWarehouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, "5");

        // 修改日期格式
        dateTimeFormat(list4);
        dateFormat(list5);
        Map<String, Object> map = new HashMap<>();
        map.put("type0", list0);
        map.put("type1", list1);
        map.put("type2", list2);
        map.put("type3", list3);
        map.put("type4", list4);
        map.put("type5", list5);
        return R.ok(map);
    }

    @Override
    public R getWarehouseStatistics(String skuclassId, String skuclassValue, String wareHouseID, String wareHouseValue, String skuID, String skuIDValue, String batchNo, String batchNoValue, String expDateNum, String expDateValue, String resetDateNum, String resetDateValue, String stockNum, String stocValue, String skuState, String skuStateValue, String sType) {
        if (!StringUtils.isEmpty(sType)) {
            return R.ok(warehouseMapper.getWarehouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, sType));
        }
        List<HashMap<String, Object>> list0 = warehouseMapper.getWarehouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, "0");
        List<HashMap<String, Object>> list1 = warehouseMapper.getWarehouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, "1");
        List<HashMap<String, Object>> list2 = warehouseMapper.getWarehouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, "2");
        List<HashMap<String, Object>> list3 = warehouseMapper.getWarehouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, "3");
        List<HashMap<String, Object>> list4 = warehouseMapper.getWarehouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, "4");
        //获取请验管理数据过滤掉待验>0的数据
//        List<HashMap<String, Object>> list5 = warehouseMapper.getWarehouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, "5");
        List<HashMap<String, Object>> list5 = warehouseMapper.getMaterialVerify(skuID, skuIDValue, batchNo, batchNoValue);

        // 修改日期格式
        list4.parallelStream().forEach(item -> {
            if (!Objects.isNull(item.get("retest_date")))
                item.put("retest_date", DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", (Date) item.get("retest_date")));
            if (!Objects.isNull(item.get("pd_date")))
                item.put("pd_date", DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", (Date) item.get("pd_date")));
            if (!Objects.isNull(item.get("exp_date")))
                item.put("exp_date", DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", (Date) item.get("exp_date")));
        });
        List<Integer> idList = new ArrayList<>();
        list5.parallelStream().forEach(item -> {
            if (!Objects.isNull(item.get("retest_date"))) {
                String pdDateStr = item.get("retest_date").toString();
                if(StringUtils.isNotBlank(pdDateStr)){
                    try {
                        Date pdDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(pdDateStr);
                        item.put("retest_date", new SimpleDateFormat("yyyy-MM-dd").format(pdDate));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }
            if (!Objects.isNull(item.get("pd_date"))) {
                String pdDateStr = item.get("pd_date").toString();
                if(StringUtils.isNotBlank(pdDateStr)){
                    try {
                        Date pdDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(pdDateStr);
                        item.put("pd_date", new SimpleDateFormat("yyyy-MM-dd").format(pdDate));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }

            }
            if (!Objects.isNull(item.get("exp_date"))) {
                String pdDateStr = item.get("exp_date").toString();
                if(StringUtils.isNotBlank(pdDateStr)){
                    try {
                        Date pdDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(pdDateStr);
                        item.put("exp_date", new SimpleDateFormat("yyyy-MM-dd").format(pdDate));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }
//            idList.add((Integer.valueOf(String.valueOf(item.get("id")))));
        });
        if (CollectionUtil.isNotEmpty(idList)) {
            //获取待待检数量为0的数据
            List<WmsStoStockStateEntity> wmsStoStockEntities = wmsStoStockStateMapper.findQuarantinenum0(idList);
            List<Long> stringList = wmsStoStockEntities.stream().map(WmsStoStockStateEntity::getStockId).collect(Collectors.toList());
            //过滤掉待检数量为0的数据，待检数量为0的数据在请验管理中不显示
            Iterator<HashMap<String, Object>> iterator = list5.iterator();
            while (iterator.hasNext()) {
                HashMap<String, Object> next = iterator.next();
                if (!stringList.contains(Long.valueOf(String.valueOf(next.get("id"))))) {
                    iterator.remove();
                }
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("type0", list0);
        map.put("type1", list1);
        map.put("type2", list2);
        map.put("type3", list3);
        map.put("type4", list4);
        map.put("type5", list5);
        return R.ok(map);
    }

    @Override
    public R getWarehouseBasic(String sType) {
        if (!StringUtils.isEmpty(sType)) {
            return R.ok(warehouseMapper.getWarehouseBasic(sType));
        }
        List<HashMap<String, Object>> list1 = warehouseMapper.getWarehouseBasic("1");
        List<HashMap<String, Object>> list2 = warehouseMapper.getWarehouseBasic("2");
        List<HashMap<String, Object>> list3 = warehouseMapper.getWarehouseBasic("3");

        HashMap<String, Object> map = new HashMap<>();
        map.put("type1", list1);
        map.put("type2", list2);
        map.put("type3", list3);

        return R.ok(map);
    }

    @Override
    public R getBillStatusAll() {
        return R.ok(warehouseMapper.getBillStatusAll());
    }

    @Override
    public R findTurnoverStatistic(String beginDate, String rndDate, String skuClass, String skuClassValue, String wareHouseName, String wareHouseValue, String skuID, String skuIDValue, String batchNo, String batchNoValue, String skuState, String sType) {
        if (!StringUtils.isEmpty(sType))
            return R.ok(warehouseMapper.findTurnoverStatistic(beginDate, rndDate, skuClass, skuClassValue, wareHouseName, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, skuState, sType));

        List<HashMap<String, Object>> list0 = warehouseMapper.findTurnoverStatistic(beginDate, rndDate, skuClass, skuClassValue, wareHouseName, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, skuState, "0");
        List<HashMap<String, Object>> list1 = warehouseMapper.findTurnoverStatistic(beginDate, rndDate, skuClass, skuClassValue, wareHouseName, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, skuState, "1");

        HashMap<String, Object> map = new HashMap<>();
        map.put("type0", list0);
        map.put("type1", list1);

        return R.ok(map);
    }

    @Override
    public List<SkuQueryExportDataVo> skuQueryExportData(String skuclassId, String skuclassValue, String wareHouseID, String wareHouseValue, String skuID, String skuIDValue, String batchNo, String batchNoValue, String expDateNum, String expDateValue, String resetDateNum, String resetDateValue, String stockNum, String stocValue, String skuState, String skuStateValue, String sType, Integer pageNum, Integer pageSize) {
        List<HashMap<String, Object>> mapList = warehouseMapper.getWarehouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, sType);
        TableDataInfo tableInfo = DataProcessCommonUtils.getTableInfo(mapList, pageNum, pageSize);
        return convertSkuQueryMapToVoList(tableInfo.getRows());
    }
    /**
     * 安全地将Map列表转换为ExportSkuAlarmsVo列表
     * @param rows 原始数据行
     * @return 转换后的VO列表
     */
    private List<SkuQueryExportDataVo> convertSkuQueryMapToVoList(Object rows) {
        if (rows == null) {
            return Collections.emptyList();
        }

        if (!(rows instanceof List<?>)) {
            return Collections.emptyList();
        }

        List<?> rowList = (List<?>) rows;
        List<SkuQueryExportDataVo> resultList = new ArrayList<>(rowList.size());

        for (Object item : rowList) {
            if (item instanceof Map<?, ?>) {
                Map<?, ?> rowMap = (Map<?, ?>) item;
                SkuQueryExportDataVo vo = new SkuQueryExportDataVo();
                vo.setCode(getStringValue(rowMap, "code"));
                vo.setGMP(getStringValue(rowMap, "GMP"));
                vo.setVbatchcode(getStringValue(rowMap, "vbatchcode"));
                vo.setVbillcode(getStringValue(rowMap, "vbillcode"));
                vo.setVvendbatchcode(getStringValue(rowMap, "vvendbatchcode"));
                vo.setSender_type(getStringValue(rowMap, "sender_type"));
                vo.setPack_version(getStringValue(rowMap, "pack_version"));
                vo.setName(getStringValue(rowMap, "name"));
                vo.setSto_unit_id(getStringValue(rowMap, "sto_unit_id"));
                vo.setMaterialspec(getStringValue(rowMap, "materialspec"));
                vo.setNcc_stu_name(getStringValue(rowMap, "ncc_stu_name"));
                vo.setPd_date(formatDate(rowMap.get("pd_date")));
                vo.setExp_date(formatDate(rowMap.get("exp_date")));
                vo.setCwarehouse_name(getStringValue(rowMap, "cwarehouse_name"));
                vo.setFreeze_num(rowMap.get("freeze_num").toString());
                vo.setRetest_date(formatDate(rowMap.get("retest_date")));
                vo.setUnqualified_num(rowMap.get("unqualified_num").toString());
                vo.setQuarantine_num(rowMap.get("quarantine_num").toString());
                vo.setSku_no_state_num(rowMap.get("sku_no_state_num").toString());
                vo.setLimit_eligibility_num(rowMap.get("limit_eligibility_num").toString());
                vo.setQualified_num(rowMap.get("qualified_num").toString());
                vo.setResearch_num(rowMap.get("research_num").toString());
                vo.setQualified_no_use_num(rowMap.get("qualified_no_use_num").toString());
                vo.setPending_num(rowMap.get("pending_num").toString());
                resultList.add(vo);
            }
        }
        return resultList;
    }
    private List<WarehouseExportVo> convertWareHouseMapToVoList(Object rows) {
        if (rows == null) {
            return Collections.emptyList();
        }

        if (!(rows instanceof List<?>)) {
            return Collections.emptyList();
        }

        List<?> rowList = (List<?>) rows;
        List<WarehouseExportVo> resultList = new ArrayList<>(rowList.size());

        for (Object item : rowList) {
            if (item instanceof Map<?, ?>) {
                Map<?, ?> rowMap = (Map<?, ?>) item;
                WarehouseExportVo vo = new WarehouseExportVo();
                vo.setCode(getStringValue(rowMap, "code"));
                vo.setGMP(getStringValue(rowMap, "GMP"));
                vo.setSender_type(getStringValue(rowMap, "sender_type"));
                vo.setVbillcode(getStringValue(rowMap, "vbillcode"));
                vo.setVbatchcode(getStringValue(rowMap, "vbatchcode"));
                vo.setVvendbatchcode(getStringValue(rowMap, "vvendbatchcode"));
                vo.setName(getStringValue(rowMap, "name"));
                vo.setMaterialspec(getStringValue(rowMap, "materialspec"));
                vo.setCwarehouse_name(getStringValue(rowMap, "cwarehouse_name"));
                vo.setSto_unit_id(getStringValue(rowMap, "sto_unit_id"));
                vo.setPd_date(formatDate(rowMap.get("pd_date")));
                vo.setExp_date(formatDate(rowMap.get("exp_date")));
                vo.setRetest_date(formatDate(rowMap.get("retest_date")));
                vo.setStateValue(getStringValue(rowMap,"stateValue"));
                vo.setUnqualified_num(rowMap.get("unqualified_num").toString());
                vo.setQuarantine_num(rowMap.get("quarantine_num").toString());
                vo.setSku_no_state_num(rowMap.get("sku_no_state_num").toString());
                vo.setLimit_eligibility_num(rowMap.get("limit_eligibility_num").toString());
                vo.setQualified_num(rowMap.get("qualified_num").toString());
                vo.setResearch_num(rowMap.get("research_num").toString());
                vo.setQualified_no_use_num(rowMap.get("qualified_no_use_num").toString());
                vo.setPending_num(rowMap.get("pending_num").toString());
                vo.setEnable_num(rowMap.get("enable_num").toString());
                vo.setSku_num(rowMap.get("sku_num").toString());
                vo.setFreeze_num(rowMap.get("freeze_num").toString());
                resultList.add(vo);
            }
        }
        return resultList;
    }


    @Override
    public List<WarehouseExportVo> exportWareHouseStatistic(String skuclassId, String skuclassValue, String wareHouseID,
                                                            String wareHouseValue, String skuID, String skuIDValue,
                                                            String batchNo, String batchNoValue, String expDateNum,
                                                            String expDateValue, String resetDateNum, String resetDateValue,
                                                            String stockNum, String stocValue, String skuState,
                                                            String skuStateValue, Integer pageNum, Integer pageSize,
                                                            HttpServletResponse response) {

        List<HashMap<String, Object>> mapList = warehouseMapper.getWarehouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID,
                skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum,
                stocValue, skuState, skuStateValue, "4");
        TableDataInfo tableInfo = DataProcessCommonUtils.getTableInfo(mapList, pageNum, pageSize);
        return convertWareHouseMapToVoList(tableInfo.getRows());
    }

    @Override
    public List<WarehouseExportVo> exportPdfWareHouseStatistic(String skuclassId, String skuclassValue, String wareHouseID,
                                                               String wareHouseValue, String skuID, String skuIDValue,
                                                               String batchNo, String batchNoValue, String expDateNum,
                                                               String expDateValue, String resetDateNum, String resetDateValue,
                                                               String stockNum, String stocValue, String skuState,
                                                               String skuStateValue, Integer pageNum, Integer pageSize,
                                                               HttpServletResponse response) {
        List<HashMap<String, Object>> mapList = warehouseMapper.getWarehouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID,
                skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum,
                stocValue, skuState, skuStateValue, "4");
        TableDataInfo tableInfo = DataProcessCommonUtils.getTableInfo(mapList, pageNum, pageSize);
        return convertWareHouseMapToVoList(tableInfo.getRows());

    }

    @Override
    public List<WarehouseExportVo> exportCsvWareHouseStatistic(String skuclassId, String skuclassValue, String wareHouseID,
                                                               String wareHouseValue, String skuID, String skuIDValue,
                                                               String batchNo, String batchNoValue, String expDateNum,
                                                               String expDateValue, String resetDateNum, String resetDateValue,
                                                               String stockNum, String stocValue, String skuState,
                                                               String skuStateValue, Integer pageNum, Integer pageSize,
                                                               HttpServletResponse response) {
        List<HashMap<String, Object>> mapList = warehouseMapper.getWarehouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID,
                skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum,
                stocValue, skuState, skuStateValue, "4");
        TableDataInfo tableInfo = DataProcessCommonUtils.getTableInfo(mapList, pageNum, pageSize);
        return convertWareHouseMapToVoList(tableInfo.getRows());
    }

    /**
     * 获取人员kpi信息
     *
     * @param dataType
     * @param sType
     * @return
     */
    @Override
    public R wmsUserKpi(String dataType, String sType) {
        if (!StringUtils.isEmpty(sType)) return R.ok(warehouseMapper.wmsUserKpi(dataType, sType));
//        Map<String, Object> map = new HashMap<>();
        List<HashMap<String, Object>> type1 = warehouseMapper.wmsUserKpi(dataType,"");
//        List<HashMap<String, Object>> type2 = warehouseMapper.wmsUserKpi(dataType, "2");
//        map.put("type1", type1);
//        map.put("type2", type2);

        return R.ok(type1);
    }

    /**
     * 出库明细
     *
     * @param beginDate
     * @param endDate
     * @param skuClass
     * @param skuClassValue
     * @param wareHouseName
     * @param wareHouseValue
     * @param skuId
     * @param skuIdValue
     * @param batchNo
     * @param batchNoValue
     * @return
     */
    @Override
    public R getWMSWarehouseDetail(@RequestParam(required = false, defaultValue = "") String beginDate, String endDate, String skuClass, String skuClassValue, String wareHouseName, String wareHouseValue, String skuId, String skuIdValue, String batchNo, String batchNoValue) {
        List<HashMap<String, Object>> list = warehouseMapper.getWMSWarehouseDetail(beginDate, endDate, skuClass, skuClassValue, wareHouseName, wareHouseValue, skuId, skuIdValue, batchNo, batchNoValue);

        list.parallelStream().forEach(item -> {
            /**
             * 以下三字段从存储过程中挪过来（备注：存储过程中是写死的）
             */
            item.put("opreator", "admin");
            item.put("from_num", "0");
            item.put("remark", "");
            if (Objects.nonNull(item.get("retest_date"))) {
                String dateStr = item.get("retest_date").toString();
                if (isFormat(dateStr)) {
                    item.put("retest_date", dateFormat(dateStr));
                } else {
                    item.put("retest_date", DateUtil.parse(item.get("retest_date").toString(), "yyyy-MM-dd HH:mm:ss").toString());
                }
            }
            if (Objects.nonNull( item.get("pd_date"))) {
                String pdStr =  item.get("pd_date").toString();
                if (isFormat(pdStr)) {
                    item.put("pd_date", dateFormat(pdStr));
                } else {
                    item.put("pd_date", DateUtil.parse( item.get("pd_date").toString(), "yyyy-MM-dd HH:mm:ss"));
                }
            }

            if (Objects.nonNull(item.get("exp_date"))) {
                String expStr = item.get("exp_date").toString();
                if (isFormat(expStr)) {
                    item.put("exp_date", dateFormat(expStr));
                } else {
                    item.put("exp_date", DateUtil.parse(item.get("exp_date").toString(), "yyyy-MM-dd HH:mm:ss"));
                }
            }
        });

        return R.ok(list);
    }

    /**
     * 判断日期字符串是否符合格式 "MM dd yyyy h:mma"
     */
    public static boolean isFormat(String dateString) {
        SimpleDateFormat format = new SimpleDateFormat("MM dd yyyy h:mma", Locale.ENGLISH);
        format.setLenient(false); // 严格模式，避免宽松解析
        try {
            format.parse(dateString);
            return true;
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     * 将MM dd yyyy h:mma处理为 yyyy-MM-dd HH:mm:ss格式
     */
    public static String dateFormat(String dateString) {
        SimpleDateFormat inputFormat = new SimpleDateFormat("MM dd yyyy h:mma", Locale.ENGLISH);
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = inputFormat.parse(dateString);
            return outputFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 安全地将Map列表转换为ExportSkuAlarmsVo列表
     * @param rows 原始数据行
     * @return 转换后的VO列表
     */
    private List<ExportWMSWarehouseOutDetailVo> convertAreaHouseDetailMapToVoList(Object rows) {
        if (rows == null) {
            return Collections.emptyList();
        }

        if (!(rows instanceof List<?>)) {
            log.error("期望List类型数据，实际类型: {}", rows.getClass().getName());
            return Collections.emptyList();
        }

        List<?> rowList = (List<?>) rows;
        List<ExportWMSWarehouseOutDetailVo> resultList = new ArrayList<>(rowList.size());

        for (Object item : rowList) {
//            if (item instanceof Map<?, ?>) {
//                Map<?, ?> rowMap = (Map<?, ?>) item;
//                ExportWMSWarehouseOutDetailVo vo = new ExportWMSWarehouseOutDetailVo();
//                vo.setWms_create_time(formatDate(rowMap.get("wms_create_time")));
//                vo.setVbillcode(getStringValue(rowMap,"vbillcode"));
//                vo.setCode(getStringValue(rowMap,"code"));
//                vo.setGraphid(getStringValue(rowMap,"graphid"));
//                vo.setSender_type(getStringValue(rowMap,"sender_type"));
//                vo.setName(getStringValue(rowMap,"name"));
//                vo.setClassName(getStringValue(rowMap, "ClassName"));
//                vo.setMaterialspec(getStringValue(rowMap, "materialspec"));
//                vo.setCunit_name(getStringValue(rowMap, "cunit_name"));
//                vo.setCwarehouse_name(getStringValue(rowMap, "cwarehouse_name"));
//                vo.setVbatchcode(getStringValue(rowMap, "vbatchcode"));
//                vo.setCproduct_name(getStringValue(rowMap, "cproduct_name"));
//                vo.setDrugTempInfo(getStringValue(rowMap,"DrugTempInfo"));
//                vo.setFromSkuNum(rowMap.get("FromSkuNum").toString());
//                vo.setToSkuNum(rowMap.get("ToSkuNum").toString());
//                vo.setInventory_num(rowMap.get("inventory_num").toString());
//                vo.setCustomer(getStringValue(rowMap,"Customer"));
//                vo.setExp_date(getStringValue(rowMap,"exp_date"));
//                vo.setRetest_date(getStringValue(rowMap,"retest_date"));
//                vo.setOperatorName(getStringValue(rowMap,"OperatorName"));
//                vo.setBillsTypeName(getStringValue(rowMap,"billsTypeName"));
//                vo.setSecondUserName(getStringValue(rowMap, "SecondUserName"));
//                vo.setCheckUserName(getStringValue(rowMap, "CheckUserName"));
//                vo.setInspection_state(getStringValue(rowMap, "inspection_state"));
//                vo.setHxsm(getStringValue(rowMap, "hxsm"));
//                vo.setRemark(getStringValue(rowMap, "remark"));
//                resultList.add(vo);
//            }
        }
        return resultList;
    }
    /**
     * 出库明细导出
     *
     * @param beginDate
     * @param endDate
     * @param skuClass
     * @param skuClassValue
     * @param wareHouseName
     * @param wareHouseValue
     * @param skuId
     * @param skuIdValue
     * @param batchNo
     * @param batchNoValue
     * @param response
     */
    @Override
    public AjaxResult exportWMSWarehouseDetail(String beginDate, String endDate, String skuClass, String skuClassValue, String wareHouseName, String wareHouseValue, String skuId, String skuIdValue, String batchNo, String batchNoValue, Integer pageNum,Integer pageSize,HttpServletResponse response) {
        List<HashMap<String, Object>> mapList = warehouseMapper.getWMSWarehouseDetail(beginDate, endDate, skuClass, skuClassValue, wareHouseName, wareHouseValue, skuId, skuIdValue, batchNo, batchNoValue);
        TableDataInfo tableInfo = DataProcessCommonUtils.getTableInfo(mapList, pageNum, pageSize);
        List<ExportWMSWarehouseOutDetailVo> list = convertAreaHouseDetailMapToVoList(tableInfo.getRows());
        ExcelUtil<ExportWMSWarehouseOutDetailVo> util = new ExcelUtil<>(ExportWMSWarehouseOutDetailVo.class);
        util.exportExcel(response, list, "出入库明细", "出入库明细", "WMSWarehouseDetail");
        return AjaxResult.success();
    }

    @Override
    public AjaxResult exportPdfWMSWarehouseDetail(String beginDate, String endDate, String skuClass, String skuClassValue, String wareHouseName, String wareHouseValue, String skuId, String skuIdValue, String batchNo, String batchNoValue,  Integer pageNum,Integer pageSize,HttpServletResponse response) {
        List<HashMap<String, Object>> mapList = warehouseMapper.getWMSWarehouseDetail(beginDate, endDate, skuClass, skuClassValue, wareHouseName, wareHouseValue, skuId, skuIdValue, batchNo, batchNoValue);
        TableDataInfo tableInfo = DataProcessCommonUtils.getTableInfo(mapList, pageNum, pageSize);
        List<ExportWMSWarehouseOutDetailVo> list = convertAreaHouseDetailMapToVoList(tableInfo.getRows());
        ExcelPDFUtil<ExportWMSWarehouseOutDetailVo> pdfUtil = new ExcelPDFUtil<>(ExportWMSWarehouseOutDetailVo.class);
        return pdfUtil.exportPDF(response, list, "出入库明细", "出入库明细", getUsername(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),"全量");
    }

    @Override
    public List<ExportWMSWarehouseOutDetailVo> exportCsvWMSWarehouseDetail(String beginDate, String endDate, String skuClass, String skuClassValue, String wareHouseName, String wareHouseValue, String skuId, String skuIdValue, String batchNo, String batchNoValue, Integer pageNum, Integer pageSize, HttpServletResponse response) {
        List<HashMap<String, Object>> mapList = warehouseMapper.getWMSWarehouseDetail(beginDate, endDate, skuClass, skuClassValue, wareHouseName, wareHouseValue, skuId, skuIdValue, batchNo, batchNoValue);
        TableDataInfo tableInfo = DataProcessCommonUtils.getTableInfo(mapList, pageNum, pageSize);
        return convertAreaHouseDetailMapToVoList(tableInfo.getRows());
    }

    // 校验是否以W开头，后面跟着三个数字
    public static boolean isValid(String input) {
        // 校验是否为两位
        return input != null;
    }

    public String getOneId() {
        Long aLong = warehouseMapper.queryBigNumber();
        if (aLong == null) {
            return "100000000";
        } else {
            aLong = aLong + 1;
            return aLong + "";
        }
    }

    /**
     * 生产日期 有效期 复验期 日期格式处理(年月日格式)
     *
     * @param list
     */
    private void dateFormat(List<HashMap<String, Object>> list) {
        list.parallelStream().forEach(item -> {
            if (!Objects.isNull(item.get("retest_date"))) {
                item.put("retest_date", DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", (Date) item.get("retest_date")));
            }
            if (!Objects.isNull(item.get("pd_date"))) {
                String pdDateStr = item.get("pd_date").toString();
                try {
                    Date pdDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(pdDateStr);
                    item.put("pd_date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(pdDate));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            if (!Objects.isNull(item.get("exp_date"))) {
                String expDateStr = item.get("exp_date").toString();
                try {
                    Date expDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(expDateStr);
                    item.put("exp_date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(expDate));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * 生产日期 有效期 复验期 日期格式处理(年月日 时分秒格式)
     *
     * @param list
     */
    private void dateTimeFormat(List<HashMap<String, Object>> list) {
        list.parallelStream().forEach(item -> {
            if (!Objects.isNull(item.get("retest_date"))) {
                item.put("retest_date", DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", (Date) item.get("retest_date")));
            }
            if (!Objects.isNull(item.get("pd_date"))) {
                String pdDateStr = item.get("pd_date").toString();
                try {
                    Date pdDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(pdDateStr);
                    item.put("pd_date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(pdDate));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            if (!Objects.isNull(item.get("exp_date"))) {
                String expDateStr = item.get("exp_date").toString();
                try {
                    Date expDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(expDateStr);
                    item.put("exp_date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(expDate));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public R printSkuInfo(PrintSkuInfoVo vo) {
        return null;
    }

    /**
     * 从Map中获取字符串值，并进行空值处理
     */
    private String getStringValue(Map<?, ?> map, String key) {
        Object value = map.get(key);
        if (value == null) {
            return "";
        }
        return String.valueOf(value);
    }

    /**
     * 安全地格式化日期
     */
    private String formatDate(Object dateObj) {
        if (dateObj == null) {
            return "";
        }
        if (dateObj instanceof Date) {
            return DateUtils.parseDateToStr("yyyy-MM-dd", (Date) dateObj);
        }
        // 处理可能的时间戳或其他格式
        try {
            if (dateObj instanceof Number) {
                Date date = new Date(((Number) dateObj).longValue());
                return DateUtils.parseDateToStr("yyyy-MM-dd", date);
            }
            // 尝试解析字符串日期
            if (dateObj instanceof String) {
                Date date = DateUtils.parseDate(dateObj.toString());
                return DateUtils.parseDateToStr("yyyy-MM-dd", date);
            }
        } catch (Exception e) {
            log.warn("日期解析失败: {}", e.getMessage());
        }
        return "";
    }
}
